<?php
/**
 * Created by PhpStorm.
 * User: yshang15
 * Date: 16/7/3
 * Time: 上午9:13
 */

namespace frontend\controllers\brokerinfo;

use Yii;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use common\models\Mobile;

class AngejiaController extends Controller
{
    public function actionIndex()
    {

        // 上海经纪人链接http://sh.angejia.com/broker?page=1   总页数25
        // 北京经纪人链接http://bj.angejia.com/broker?page=1   总页数3
        $city = 'sh';
        if ($city == 'sh') {
            $total_page = 25;
        } elseif ($city == 'bj') {
            $total_page = 3;
        }
        for($page = 1;$page <= $total_page; $page ++) {
            $str_brokers = file_get_contents("http://".$city.".angejia.com/broker?page=$page");
            preg_match_all('/http:\/\/'.$city.'.angejia.com\/broker\/\d+.html/', $str_brokers, $match);
            if ($match[0]) {
                foreach($match[0] as $broker_url) {
                    $str_broker = file_get_contents($broker_url);
                    preg_match_all('/<li class="clearfix broker-name" .*><h3>(.*)<\/h3>.*(<\/li>)?/', $str_broker, $mch_name);
                    $broker_name = (isset($mch_name[1][0]) && $mch_name[1][0]) ? $mch_name[1][0] : '';
                    $str_broker = str_replace(' ', '', $str_broker);
                    $str_broker = str_replace("\n", '', $str_broker);
                    $str_broker = str_replace("\r", '', $str_broker);
                    preg_match_all('/<strong>微信号：<\/strong><span>(.*)<\/span><\/div>/U', $str_broker, $mch_wechat);
                    $broker_wechat = (isset($mch_wechat[1][0]) && $mch_wechat[1][0]) ? $mch_wechat[1][0] : '';
                    if (empty($broker_wechat)) continue;
                    $arr_wechat = Mobile::findOne(['wechat' => $broker_wechat]);
                    if (empty($arr_wechat)) {
                        $obj = new Mobile();
                        $obj->wechat = $broker_wechat;
                        $obj->name = $broker_name;
                        $obj->city = $city;
                        $obj->souce = 1;
                        $obj->up_time = date('Y-m-d H:i:s');
                        $obj->save();
                    }
                }
            }
        }
    }
}